<?php

/**
 * red_de_sensores actions.
 *
 * @package    sensores
 * @subpackage red_de_sensores
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class red_de_sensoresActions extends sfActions
{
  public function executeMenu_operaciones(sfWebRequest $request) {}
  public function executeMenu_graficos(sfWebRequest $request) {}
  
  public function executeIndex(sfWebRequest $request){}
  public function executeListado(sfWebRequest $request)
  {
    $this->red_sensoress = Doctrine_Core::getTable('RedSensores')
      ->createQuery('a')
      ->execute();
  }
/*******************************************************************************/
  
  
  
  public function executeEliminar_total(sfWebRequest $request){}
  
  public function executeEliminar(sfWebRequest $request)
  {
      Doctrine::getTable('Nodo1')->createQuery('a')->delete();
      $this->redirect('nodo1/listado');
  }
  public function executeRed_sensor(sfWebRequest $request){}
  
  public function executeDatosgrafica(sfWebRequest $request)
  {        
	$categorias = array('nombre');
	$sensor1 = array('mota1');
	$sensor2 = array('mota2');
               
        $result = Doctrine_Core::getTable('RedSensores')->createQuery('b')->select('b.nombre')->addWhere("b.nombre = 'sensor_1'")->orWhere("b.nombre = 'sensor_2'")->execute();
        foreach($result as $item):
            $categorias[] = $item->getNombre();
        endforeach;
        
        $consulta = Doctrine_Core::getTable('RedSensores')->createQuery('b')->select('b.nombre, b.mota1, b.mota2')->execute();
        foreach($consulta as $elemento):
             if($elemento->getNombre() == 'sensor_1'):
                $sensor1[] = (double) $elemento->getMota1();
                $sensor1[] = (double) $elemento->getMota2();
             elseif($elemento->getNombre() == 'sensor_2'):
                $sensor2[] = (double) $elemento->getMota1();
                $sensor2[] = (double) $elemento->getMota2();                
             endif;            
        endforeach;
        //$conexion = new mysqli('localhost','root','10495270','datos_sensor');	
        //print_r($sensor1);
        //print_r($sensor2);
        //$consulta = "SELECT DISTINCT(nombre) FROM red_sensores WHERE nombre = 'sensor_1' OR nombre = 'sensor_2' ORDER BY nombre";
	//$result = $conexion->query($consulta);
        //	while ($fila = $result->fetch_array()) 
        //{
//            $categorias[] = $fila['nombre'];
//	}	
	//$consulta = "SELECT nombre, mota1, mota2 FROM red_sensores;";        
	//$result = $conexion->query($consulta);	
//        while($fila = $result->fetch_array())
//	{				
//			if ( $fila['nombre'] == 'sensor_1')
//			{
//				$sensor1[] = (double) $fila['mota1'];
//				$sensor1[] = (double) $fila['mota2'];
//			}
//			elseif ( $fila['nombre'] == 'sensor_2')
//			{
//				$sensor2[] = (double) $fila['mota1'];
//				$sensor2[] = (double) $fila['mota2'];
//			}	
//			
//	}
	
        echo json_encode( array($categorias, $sensor1, $sensor2) );
    }
  

  public function executeMitabla(sfWebRequest $request){}  
  
  
  public function executeDatostabla(sfWebRequest $request)
  {        
	$categorias = array('nombre');
	$trama1 = array('valor1');
	$trama2 = array('valor2');        
        $trama3 = array('valor3');
        $trama4 = array('valor4');
        
               
        //$result = Doctrine_Core::getTable('Nodo1')->createQuery('b')->select('b.nombre')->addWhere("b.nombre = 'trama_1'")->orWhere("b.nombre = 'trama_2'")->execute();
        $result = Doctrine_Core::getTable('Nodo1')->createQuery('b')->select('b.nombre')->addWhere("b.nombre = 'trama_1'")->orWhere("b.nombre = 'trama_2'")->execute();
        foreach($result as $item):
            $categorias[] = $item->getNombre();
        endforeach;
        
        $consulta = Doctrine_Core::getTable('Nodo1')->createQuery('b')->select('b.nombre, b.valor1, b.valor2, b.valor3, b.valor4')->execute();
        foreach($consulta as $elemento):
             if($elemento->getNombre() == 'trama_1'):
                $trama1[] = (double) $elemento->getValor1();
                $trama1[] = (double) $elemento->getValor2();
                //$trama1[] = (double) $elemento->getValor3();
             elseif($elemento->getNombre() == 'trama_2'):
                $trama2[] = (double) $elemento->getValor1();
                $trama2[] = (double) $elemento->getValor2();                
                //$trama1[] = (double) $elemento->getValor3();
             elseif($elemento->getNombre() == 'trama_3'):
                $trama3[] = (double) $elemento->getValor3();             
                $trama3[] = (double) $elemento->getValor4();
             elseif($elemento->getNombre() == 'trama_4'):
                $trama4[] = (double) $elemento->getValor4();
             $trama4[] = (double) $elemento->getValor5();
             endif;            
        endforeach;
        
        //echo json_encode( array($categorias, $trama1, $trama2) );
        echo json_encode( array($categorias, $trama1, $trama2, $trama3, $trama4) );
    }
    
  
  public function executeRecuperadatos(sfWebRequest $request)
  {
     $params["tqx"] = $request->getParameter('tqx');
     
          
     list($param[], $reqId) = explode(":", $params["tqx"]);
 
    $this->dat    = Doctrine_Query::create()->select(" rs.mota1 ")->from(" RedSensores rs ")->groupBy(" rs.nombre")->execute();
 
    // Construcción de estructura de datos que se devolverá en JSON
    
    $output = "google.visualization.Query.setResponse({'$param[0]':'$reqId', 'status':'OK',";
    $output .= "'table': {cols: [{id:'nombre',label:'Mombre',type:'string'}"
                      .",{id:'mota1', label:'Mota_uno', type:'number'}"
                      .",{id:'mota2', label:'Mota_dos', type:'number'}]";
    $output .= ",rows: [";
    $separator = "";    
 
    $datos = $this->dat;
    foreach($datos as $dato)
    {
      $output .= $separator . "{c:[{v:'" . $dato["nombre"] . "'},{v:" . $dato["mota1"] . ",f:'" . $dato["mota2"] . "'},{v:false}]}";
      $separator = ",";
    }
    $output .= "]}});";
 
    // Se establece el header de la respuesta.    
    $this->salida = $output;
    
    $this->getResponse()->setHttpHeader('Content-type', 'text/plain');
 
    return $this->renderText($output); 
  }
/*******************************************************************************/
  
  public function executeShow(sfWebRequest $request)
  {
    $this->red_sensores = Doctrine_Core::getTable('RedSensores')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->red_sensores);
  }
  
  public function executePrueba(sfWebRequest $request)
  {}

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new RedSensoresForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new RedSensoresForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($red_sensores = Doctrine_Core::getTable('RedSensores')->find(array($request->getParameter('id'))), sprintf('Object red_sensores does not exist (%s).', $request->getParameter('id')));
    $this->form = new RedSensoresForm($red_sensores);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($red_sensores = Doctrine_Core::getTable('RedSensores')->find(array($request->getParameter('id'))), sprintf('Object red_sensores does not exist (%s).', $request->getParameter('id')));
    $this->form = new RedSensoresForm($red_sensores);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($red_sensores = Doctrine_Core::getTable('RedSensores')->find(array($request->getParameter('id'))), sprintf('Object red_sensores does not exist (%s).', $request->getParameter('id')));
    $red_sensores->delete();

    $this->redirect('red_de_sensores/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $red_sensores = $form->save();

      $this->redirect('red_de_sensores/edit?id='.$red_sensores->getId());
    }
  }
}
